package com.ruoyi.reportsnhnt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.reportsnhnt.domain.ReportSnhntKytxmlBaDetail;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntKytxmlBaDetailMapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportsnhnt.domain.bo.ReportSnhntKytxmlBsInfoBo;
import com.ruoyi.reportsnhnt.domain.vo.ReportSnhntKytxmlBsInfoVo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntKytxmlBsInfo;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntKytxmlBsInfoMapper;
import com.ruoyi.reportsnhnt.service.IReportSnhntKytxmlBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 混凝土抗压弹性模量试验检测记录(棱柱体)Service业务层处理
 *
 * @author zxs
 * @date 2024-06-21
 */
@RequiredArgsConstructor
@Service
public class ReportSnhntKytxmlBsInfoServiceImpl implements IReportSnhntKytxmlBsInfoService {

    private final ReportSnhntKytxmlBsInfoMapper baseMapper;
    private final ReportSnhntKytxmlBaDetailMapper detailMapper;

    /**
     * 查询混凝土抗压弹性模量试验检测记录(棱柱体)
     */
    @Override
    @FIllBasicInfo
    public ReportSnhntKytxmlBsInfoVo queryById(Long excelId){
        ReportSnhntKytxmlBsInfoVo vo = baseMapper.selectVoById(excelId);
        //查询子表；
        List<ReportSnhntKytxmlBaDetail> details = detailMapper.selectList(new LambdaQueryWrapper<ReportSnhntKytxmlBaDetail>().eq(ReportSnhntKytxmlBaDetail::getExcelId,excelId));
        vo.setDetails(details);
        return vo;
    }

    /**
     * 查询混凝土抗压弹性模量试验检测记录(棱柱体)列表
     */
    @Override
    public TableDataInfo<ReportSnhntKytxmlBsInfoVo> queryPageList(ReportSnhntKytxmlBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSnhntKytxmlBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportSnhntKytxmlBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询混凝土抗压弹性模量试验检测记录(棱柱体)列表
     */
    @Override
    public List<ReportSnhntKytxmlBsInfoVo> queryList(ReportSnhntKytxmlBsInfoBo bo) {
        LambdaQueryWrapper<ReportSnhntKytxmlBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSnhntKytxmlBsInfo> buildQueryWrapper(ReportSnhntKytxmlBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSnhntKytxmlBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPage() != null, ReportSnhntKytxmlBsInfo::getPage, bo.getPage());
        lqw.eq(bo.getTotalPage() != null, ReportSnhntKytxmlBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportSnhntKytxmlBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportSnhntKytxmlBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportSnhntKytxmlBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportSnhntKytxmlBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpxx()), ReportSnhntKytxmlBsInfo::getYangpxx, bo.getYangpxx());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportSnhntKytxmlBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportSnhntKytxmlBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportSnhntKytxmlBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportSnhntKytxmlBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getLaiysj() != null, ReportSnhntKytxmlBsInfo::getLaiysj, bo.getLaiysj());
        lqw.eq(bo.getChengxrq() != null, ReportSnhntKytxmlBsInfo::getChengxrq, bo.getChengxrq());
        lqw.eq(bo.getShiyjcrq() != null, ReportSnhntKytxmlBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytj()), ReportSnhntKytxmlBsInfo::getShiytj, bo.getShiytj());
        lqw.eq(StringUtils.isNotBlank(bo.getWend()), ReportSnhntKytxmlBsInfo::getWend, bo.getWend());
        lqw.eq(StringUtils.isNotBlank(bo.getXiangdsd()), ReportSnhntKytxmlBsInfo::getXiangdsd, bo.getXiangdsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportSnhntKytxmlBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportSnhntKytxmlBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportSnhntKytxmlBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportSnhntKytxmlBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getShejtxml()), ReportSnhntKytxmlBsInfo::getShejtxml, bo.getShejtxml());
        lqw.eq(StringUtils.isNotBlank(bo.getPeihbbh()), ReportSnhntKytxmlBsInfo::getPeihbbh, bo.getPeihbbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYanghtj()), ReportSnhntKytxmlBsInfo::getYanghtj, bo.getYanghtj());
        lqw.eq(StringUtils.isNotBlank(bo.getLingq()), ReportSnhntKytxmlBsInfo::getLingq, bo.getLingq());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportSnhntKytxmlBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportSnhntKytxmlBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportSnhntKytxmlBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportSnhntKytxmlBsInfo::getFuh, bo.getFuh());
        lqw.eq(bo.getRiq() != null, ReportSnhntKytxmlBsInfo::getRiq, bo.getRiq());
        return lqw;
    }

    /**
     * 新增混凝土抗压弹性模量试验检测记录(棱柱体)
     */
    @Override
    public Boolean insertByBo(ReportSnhntKytxmlBsInfoBo bo) {
        ReportSnhntKytxmlBsInfo add = BeanUtil.toBean(bo, ReportSnhntKytxmlBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            //新增子表数据三条；
            List<ReportSnhntKytxmlBaDetail> details = new ArrayList<>();
            for(int i = 0;i<3;i++){
                ReportSnhntKytxmlBaDetail detail = new ReportSnhntKytxmlBaDetail();
                detail.setExcelId(bo.getExcelId());
                details.add(detail);
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改混凝土抗压弹性模量试验检测记录(棱柱体)
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(ReportSnhntKytxmlBsInfoBo bo) {
        ReportSnhntKytxmlBsInfo update = BeanUtil.toBean(bo, ReportSnhntKytxmlBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        //更新子表；
        if(flag){
            detailMapper.updateBatchById(bo.getDetails());
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSnhntKytxmlBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除混凝土抗压弹性模量试验检测记录(棱柱体)
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
